package com.hualude.roadsign.service.impl;

import com.hualude.roadsign.dto.*;
import com.hualude.roadsign.repository.*;
import com.hualude.roadsign.service.RoadLineFaultCheckService;
import com.hualude.roadsign.utils.PushUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Created by ding on 2017/8/14.
 */
@Service
public class RoadLineFaultCheckServiceImpl implements RoadLineFaultCheckService {

    @Resource
    private RoadLineFaultRepository roadLineFaultRepository;
    @Resource
    private RoadLineRepository roadLineRepository;
    @Resource
    private UserRepository userRepository;
    @Resource
    private OrgRepository orgRepository;
    @Resource
    private PushHistoryRepository pushHistoryRepository;

    private String strUserIds;

    private SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    @Override
    public DTResponseDto getRoadLineFaultCheck4DT(DTRequestDto dtRequestDto, Integer actorId, Integer orgId) throws Exception {
        List<DTLineFaultDto> dto4DTs = roadLineFaultRepository.getRoadLineFaultCheck4DT(dtRequestDto, actorId, orgId);
        Integer count = roadLineFaultRepository.getRoadLineFaultCheckCount4DT(dtRequestDto, actorId, orgId);
        DTResponseDto dtResponseDto = new DTResponseDto(dtRequestDto.getDraw(), count, count, dto4DTs);
        for (DTLineFaultDto dtMonitorFaultDto : dto4DTs) {
            String creatorName = userRepository.getUserNameByUserId(dtMonitorFaultDto.getRoadLineFaultCreatorId());
            dtMonitorFaultDto.setRoadLineFaultCreatorName(creatorName);
            String checkerName = userRepository.getUserNameByUserId(dtMonitorFaultDto.getRoadLineCheckerId());
            dtMonitorFaultDto.setRoadLineCheckerName(checkerName);
            if (dtMonitorFaultDto.getRoadLineMainCheckerId() != null) {
                String mainCheckerName = userRepository.getUserNameByUserId(dtMonitorFaultDto.getRoadLineMainCheckerId());
                dtMonitorFaultDto.setRoadLineMainCheckerName(mainCheckerName);
            }
        }
        return dtResponseDto;
    }


    @Transactional
    public Integer roadLineFaultAudit(Integer isPass, String feedback, Integer lampFaultId, Integer userId) throws Exception {
        Date d = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateNowStr = sdf.format(d);


        Integer check = roadLineFaultRepository.getLampCheck(lampFaultId);
        //check:1,主管在审核  isPass=2 跳过主管直接维修  也要执行主管的功能
        check=(isPass==2)?1:check;
        if (check == 1) {
            if (isPass == 1||isPass==2) {
                final String[] strFaultType = {"损毁", "其他"};
                final LineFaultDto faultDto = roadLineFaultRepository.getInfoByFaultId(lampFaultId);
                final Integer orgId = faultDto.getRoadLineOrgId();
                List<UserDto> userDtos = userRepository.getUserIdsByOrgIdAndActorId(orgId, 5);
                final String msg = "标线 NO：" + faultDto.getRoadLineId() + " " +
                        strFaultType[faultDto.getRoadLineFaultType()];
                strUserIds = "";
                for (UserDto userDto : userDtos) {
                    strUserIds += userDto.getUserId().toString() + ",";
                }
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            if (!strUserIds.equals("")) {
                                strUserIds = strUserIds.substring(0, strUserIds.length() - 1);
                                PushUtil.sendPushToUser(strUserIds, msg);
                                PushHistoryDto pushHistoryDto = new PushHistoryDto(orgId, 5, msg, formatter.format(new Date()),6);
                                pushHistoryRepository.save(pushHistoryDto.toBean());
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }).start();
                if(isPass==1)
                    return roadLineFaultRepository.faultAudit2Success(isPass, feedback, lampFaultId, userId, dateNowStr);
                else
                    return roadLineFaultRepository.straightFaultAudit(isPass, feedback, lampFaultId, userId, dateNowStr);

            } else {
                return roadLineFaultRepository.faultAudit2Fail(isPass, feedback, lampFaultId, userId, dateNowStr);
            }
        } else {
            if(isPass==1){
                final LineFaultDto faultDto = roadLineFaultRepository.getInfoByFaultId(lampFaultId);
                List<UserDto> userDtos = userRepository.getUserIdsByOrgIdAndActorId(faultDto.getRoadLineMainOrgId(), 7);
                final String msg = "标线 NO：" + faultDto.getRoadLineId() + " 待审核";
                strUserIds = "";
                for (UserDto userDto : userDtos) {
                    strUserIds += userDto.getUserId().toString() + ",";
                }
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            if (!strUserIds.equals("")) {
                                strUserIds = strUserIds.substring(0, strUserIds.length() - 1);
                                PushUtil.sendPushToUser(strUserIds, msg);
                                PushHistoryDto pushHistoryDto = new PushHistoryDto(faultDto.getRoadLineMainOrgId(), 7, msg, formatter.format(new Date()),6);
                                pushHistoryRepository.save(pushHistoryDto.toBean());
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }).start();
            }
            return roadLineFaultRepository.faultAudit(isPass, feedback, lampFaultId, userId, dateNowStr);
        }
    }

    public List<LineFaultDto> getRoadLineFaultCheckList4App(Integer orgId, String actorIds, Integer faultStatus, Integer count, Integer size) throws Exception {
        return roadLineFaultRepository.getRoadLineFaultCheckList4App(orgId, actorIds, faultStatus, count, size);
    }

    public LineFaultDto getRoadLineInfo2Fault(Integer roadLineId) throws Exception {
        LineFaultDto monitorFaultDto = roadLineRepository.getRoadLineInfo2Fault(roadLineId);
        monitorFaultDto.setRoadLineCreatorName(userRepository.getUserNameByUserId(monitorFaultDto.getRoadLineCreatorId()));
        monitorFaultDto.setRoadLineOrgName(orgRepository.getMainOrgName(monitorFaultDto.getRoadLineOrgId()));
        monitorFaultDto.setRoadLineMainOrgName(orgRepository.getMainOrgName(monitorFaultDto.getRoadLineMainOrgId()));
        monitorFaultDto.setRoadLineId(roadLineId);
        return monitorFaultDto;
    }

    public LineFaultDto getRoadLineFaultInfo2Fault(Integer roadLineId, Integer roadLineFaultId) throws Exception {
        LineFaultDto signalLampFaultDto = roadLineRepository.getRoadLineInfo2Fault(roadLineId);
        LineFaultDto lineFaultDto = roadLineFaultRepository.getRoadLineFaultInfo2Fault(roadLineFaultId);
        LineFaultDto latlngDto = roadLineFaultRepository.getRoadLineLatLng(roadLineId);
        lineFaultDto.setLongitude(latlngDto.getLongitude());
        lineFaultDto.setLatitude(latlngDto.getLatitude());
        lineFaultDto.setRoadLineProvince(signalLampFaultDto.getRoadLineProvince());
        lineFaultDto.setRoadLineCity(signalLampFaultDto.getRoadLineCity());
        lineFaultDto.setRoadLineArea(signalLampFaultDto.getRoadLineArea());
        lineFaultDto.setRoadLineStartRoad(signalLampFaultDto.getRoadLineStartRoad());
        lineFaultDto.setRoadLineEndRoad(signalLampFaultDto.getRoadLineEndRoad());
        lineFaultDto.setRoadLineRoadName(signalLampFaultDto.getRoadLineRoadName());
        lineFaultDto.setRoadLineOrgId(signalLampFaultDto.getRoadLineOrgId());
        lineFaultDto.setRoadLineMainOrgId(signalLampFaultDto.getRoadLineMainOrgId());
        UserDto userDto = userRepository.getUserById(lineFaultDto.getRoadLineFaultCreatorId());
        lineFaultDto.setRoadLineFaultCreatorName(userDto.getUserName());
        lineFaultDto.setRoadLineFaultCreatorPhone(userDto.getUserMobile());
        return lineFaultDto;
    }

    public List<LineFaultDto> getRoadLineFaultReportList4App(Integer orgId, String actorIds, Integer faultStatus, Integer count, Integer size) throws Exception {
        return roadLineFaultRepository.getRoadLineFaultReportList4App(orgId, actorIds, faultStatus, count, size);
    }
}
